package com.gxa.wmcrm.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gxa.wmcrm.entity.User;
import com.gxa.wmcrm.service.Impl.UserServiceImpl;
import com.gxa.wmcrm.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;


@WebServlet(name = "crm_login", urlPatterns = "/crm_login")
public class UserServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }


    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json; charset=utf-8");

        //后台接收JSON数据
        InputStream inputStream = request.getInputStream();
        InputStreamReader reader = new InputStreamReader(inputStream, "UTF-8");
        // 字符缓冲区
        char[] buff = new char[1024];
        int length = 0;
        String jsonstr = null;
        while ((length = reader.read(buff)) != -1) {
            // 通过String的构造方法将 字符缓冲区 转化为一个String
            jsonstr = new String(buff, 0, length);
            System.out.println(jsonstr);
            //jsonstr = {login: login, pwd: pwd, code: code, AdminCode: AdminCode};
        }
        // 通过 FastJSON方法 将JSON结构的字符串转为 map
        Map<String, Object> parameters = JSONObject.parseObject(jsonstr);
        System.out.println("parameters ：" + parameters);

        String username = (String) parameters.get("login");
        String password = (String) parameters.get("pwd");
        String code = (String) parameters.get("code");
        String AdminCode = (String) parameters.get("AdminCode");

        System.out.println("-- username -- : " + username);
        System.out.println("-- password -- : " + password);
        System.out.println("-- code -- : " + code);
        System.out.println("-- AdminCode -- : " + AdminCode);


        User user = new User();
        user.setUsername(username);
        user.setPassword(password);


        PrintWriter out = response.getWriter();
        // 准备返回数据 - Map
        Map<String,Object> map = new HashMap<>();
        String message;
        // 判断用户输入的验证码 和 系统生成的验证码是否相同
        if (code.equals(AdminCode)) {
            UserService userService = new UserServiceImpl();
            int action = userService.queryUser(user);
            if (action == 1) {
                // 准备返回数据 - Map
                map.put("Status","success");
                map.put("Text","登录成功!");
                // map转字符串
                message = JSON.toJSONString(map);
                out.print(message);
            } else {
                map.put("Status","error");
                map.put("Text","登录失败！");
                message = JSON.toJSONString(map);
                out.print(message);
            }
        } else {
            map.put("Status","error");
            map.put("Text","验证码错误！");
            message = JSON.toJSONString(map);
            out.print(message);
        }
    }
}


